Ext.define('appfe.ux.StartupCode', {});
/**
 * 启动时初始化, 顺便执行一下下面的代码
 */
/**
 * 关闭浏览器时发出提示
 */
/*
window.onbeforeunload = onbeforeunload_handler;
window.onunload = onunload_handler;
function onbeforeunload_handler() {
    var warning = "确认退出?";
    return warning;
};

function onunload_handler() {
    var warning = "谢谢光临";
    alert(warning);
};
*/
/**
 * 获取屏幕大小
 */
var screenX;
var screenY;
//console.debug('screen size');
// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
if (typeof window.innerWidth != 'undefined') {
    screenX = window.innerWidth;
    screenY = window.innerHeight;
} // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
else if (typeof document.documentElement != 'undefined' &&
    typeof document.documentElement.clientWidth != 'undefined' &&
    document.documentElement.clientWidth != 0) {
    screenX = document.documentElement.clientWidth;
    screenY = document.documentElement.clientHeight;
} else // older versions of IE
{
    screenX = document.getElementsByTagName('body')[0].clientWidth;
    screenY = document.getElementsByTagName('body')[0].clientHeight;
}
//document.write('<p>Your viewport width is '+screenX+'x'+screenY+'</p>');
//-->
/**
 * [description]
 */
// Ext.Ajax.defaultHeaders = {
//     'X-Requested-Agent':'extjs'
// };

Ext.Ajax.on("beforerequest",
    function (conn, options, eOpts) {

        if (!options.headers) {
            options.headers = { 'X-Requested-Agent': 'extjs' };
            //options.headers.ccc ='asd';
            //options.headers['ddd'] ='dsdsdsd';
        } else {
            if (!options.headers['X-Requested-Agent']) {
                options.headers['X-Requested-Agent'] = 'extjs';
            }
        }
        //console.debug(options.headers);
    },
    this);
Ext.Ajax.on("requestexception",
    function (ajaxEmt1, response, options, eOpts) {
        //console.debug('Ext.Ajax.on.requestcomplete', 'Ext.Ajax.on.requestcomplete');
        var v_message = "服务器忙, 请稍候访问. 状态码:000. 请尝试退出系统重新登录";
        if (response.status == "401") {
            v_message = "认证失败, 请重新登录!";
            alert("认证失败, 请重新登录!");
            window.location = "#home";
        } else if (response.status >= 400 && response.status <= 500) {
            if (Ext.String.startsWith(response.getResponseHeader("Content-type"), "application/json",true)) {
                try {
                    var respJson = Ext.JSON.decode(response.responseText);
                    if (respJson.message) {
                        v_message = respJson.message + ", 状态码：" + response.status;
                    } else {
                        v_message = "系统服务错误, 请联系管理员. 状态码：" + response.status;
                    }
                } catch (e) {
                    v_message = "系统服务错误, 请联系管理员. 状态码：" + response.status;
                }
            } else {
                v_message = "系统服务错误, 请联系管理员. 状态码：" + response.status;
            }
        } else if (response.status == "302" || response.status == "301") {
            v_message = "请尝试退出系统重新登录";
        }
        var statusbar = Ext.getCmp("msg-statusbar");
        if (statusbar) {
            statusbar.setStatus({
                text: '<span style="color:red;font-weight:bold;">' + v_message + "</span>",
                iconCls: "x-status-error",
                clear: 10000 //Modified By Wanghaitao 采用自动消失的设置，错误信息设置为10秒
            });
        }
    },
    this);

Ext.Ajax.on("requestcomplete",
    function (conn, response, alarm_fields) {
        if (typeof response.getResponseHeader == "undefined") {
            return;
        }
        if (response.getResponseHeader("Content-type") == "application/x-msdownload") {
            return;
        }

        // 存在账号交叉登录需刷新页面，重新加载菜单权限 Modified By YangJian
        if (response.getResponseHeader("Content-type") == "text/html;charset=UTF-8" && typeof response.responseText != "undefined" && "Refresh" == response.responseText) {
            Ext.Msg.alert('提示:', '系统存在多账号交叉登陆.');
            response.responseText = "";
            window.location = "main.do";
            return;
        }

        // 存在账号交叉登录需刷新页面，重新加载菜单权限 Modified By YangJian
        if (response.getResponseHeader("Content-type") == "text/html;charset=UTF-8" && typeof response.responseText != "undefined" && "Authority" == response.responseText) {
            Ext.Msg.alert('提示:', '您没有此操作权限!');
            return;
        }

        //console.debug('response.responseText', response.responseText);
        if (response.responseText == '') {
            return;
        }
        var respJson = Ext.JSON.decode(response.responseText);
        if (respJson.message) {
            //console.debug('Ext.Ajax.on.requestcomplete', respJson.message);
            var statusbar = Ext.getCmp("msg-statusbar");
            if (statusbar && respJson.success) {
                statusbar.setStatus({
                    text: '<span style="color:green;font-weight:bold;">' + respJson.message + "</span>",
                    iconCls: "x-status-valid",
                    clear: 3000 //Modified By Wanghaitao 采用自动消失的设置，正确信息设置为3秒，如果设置为false则不自动消失，如果设置为true则使用autoClear的设置值
                });
            } else {
                if (statusbar) {
                    statusbar.setStatus({
                        text: '<span style="color:red;font-weight:bold;">' + respJson.message + "</span>",
                        iconCls: "x-status-error",
                        clear: 10000 //Modified By Wanghaitao 采用自动消失的设置，错误信息设置为10秒
                    });
                }
            }
        }
        /*
        if (response.getResponseHeader("Content-type") == "application/json;charset=UTF-8") {
        } else {
            response.responseText = "";
            window.location = "admin/logout.do"; //TODO:Session过期后的处理
        }
        */
    },
    this);